//
//  TokBox
//  TokBoxAdium
//
//  Created by Eion Robb on 19/06/08.
//  Copyright 2008 Eion Robb. All rights reserved.
//

#import "TokBoxAdium.h"
#import <WebKit/WebKit.h>
#import <WebKit/WebView.h>
#import <WebKit/WebUIDelegate.h>
#import <Adium/AIMenuControllerProtocol.h>
#import <AIUtilities/AIStringUtilities.h>
#import <Adium/AIListObject.h>
#import <Adium/AIAccount.h>
#import <Adium/AIChatControllerProtocol.h>
#import <Adium/AIChat.h>
#import <Adium/AIContentMessage.h>
#import <Adium/AIContentControllerProtocol.h>
#import <Adium/AIAccountControllerProtocol.h>
#import <Adium/AIListContact.h>
#import <Adium/AIMetaContact.h>

@implementation TokBoxAdium


- (void)installPlugin
{
	printf("Loading TokBox plugin2\n");
	mitem = [[NSMenuItem alloc] initWithTitle:AILocalizedString(@"Initiate TokBox Videochat","") action:@selector(startTokBox:) keyEquivalent:@""];
	[mitem setTarget:self];
	
	[[adium menuController] addContextualMenuItem:mitem toLocation:Context_Contact_Action];
}

- (void)startTokBox:(NSMenuItem*)item
{
	AIListContact *contact = (AIListContact*)[[adium menuController] currentContextMenuObject];
	if([contact conformsToProtocol:@protocol(AIContainingObject)])
		contact = [(AIMetaContact*)contact preferredContact];
	
	//create a new UUID
	CFUUIDRef	uuidObj = CFUUIDCreate(kCFAllocatorDefault);
	//get the string representation of the UUID
	NSString	*newUUID = (NSString*)CFUUIDCreateString(nil, uuidObj);
	CFRelease(uuidObj);
	
	NSString *url = [[NSString alloc] initWithFormat:@"http://www.tokbox.com/b/%@", newUUID];
	
	NSAttributedString *messageAttrStr = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:AILocalizedString(@"Please join me for video chat on %@ !",""),url]];
	AIAccount *account = [[adium accountController] preferredAccountForSendingContentType:CONTENT_MESSAGE_TYPE toContact:contact];
	AIChat *chat = [[adium chatController] chatWithContact:contact];
	AIContentMessage *message = [[AIContentMessage alloc] initWithChat:chat source:account destination:contact date:[NSDate date] message:messageAttrStr];
	
	if([[adium contentController] sendContentObject:message])
		[self openUrlInWindow:url];
	
	[url release];
	[messageAttrStr release];

}

- (void)openUrlInWindow:(NSString *)urlText
{
	NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlText]];
	NSRect rect = NSMakeRect (0,0,640,480);
	NSWindow *window = [[NSWindow alloc] initWithContentRect:rect styleMask:NSResizableWindowMask|NSClosableWindowMask backing:NSBackingStoreBuffered defer:NO];
	WebView *webView = [[WebView alloc] initWithFrame: rect];
	[webView setCustomUserAgent:@"Adium_TokBox (1.0)"];
	[webView setHostWindow:window];
	[webView setUIDelegate:self];
	[webView setShouldCloseWithWindow:YES];
	[[webView mainFrame] loadRequest:request];
	[window setContentView:webView];
	[window makeKeyAndOrderFront:self];
}

- (void)makeRestCall:(NSString *)urlText postData:(NSString *)postData
{
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlText]];
	
	[request setHTTPMethod:@"POST"];
	[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
	[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
	[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
	
	WebView *webView = [[WebView alloc] initWithFrame: NSMakeRect (0,0,640,480)];
	[webView setResourceLoadDelegate:self];
	[[webView mainFrame] loadRequest:request];
}

/*- (void)webView:(WebView *)sender resource:(id)identifier didFinishLoadingFromDataSource:(WebData Source *)dataSource
{
	[sender stopLoading:self];
}*/

- (void)uninstallPlugin
{
	[[adium menuController] removeMenuItem:mitem];
	[mitem release];
	mitem = nil;
}

- (NSString *)pluginAuthor {
	return @"Eion Robb";
}

- (NSString *)pluginVersion {
	return @"0.1";
}

- (NSString *)pluginDescription {
	return @"Allows video calls with people via TokBox.";
}

- (NSString *)pluginURL {
	return @"";
}

@end
